import Vue from 'vue'
import VueRouter from 'vue-router'
import Register from "@/components/LoginAndRegister/Register";
import Login from "@/components/LoginAndRegister/Login";
import Book from "@/components/Book";
import Index from "@/components/index"
import UserEdit from "@/components/User_setting/user_edit/UserEdit";
import Setting from "@/components/User_setting/UserSetting"
import AddressEdit from "@/components/User_setting/address_edit/AddressEdit"
import MyReceivingAddress from "@/components/User_setting/address_edit/receiving_address/MyReceivingAddress"
import ReceivingAddressEdit from "@/components/User_setting/address_edit/receiving_address/ReceivingAddressEdit";
import BookDetail from "@/components/book/BookDetail"
import MyPostAddress from "@/components/User_setting/address_edit/post_address/MyPostAddress";
import PostAddressEdit from "@/components/User_setting/address_edit/post_address/PostAddressEdit";
import ReturnAddressEdit from "@/components/User_setting/address_edit/return_address/ReturnAddressEdit";
import MyReturnAddress from "@/components/User_setting/address_edit/return_address/MyReturnAddress";
import Account_safety from "@/components/User_setting/account_safety/account_safety";
import AccountCancellation from "@/components/User_setting/account_safety/account_cancellation/AccountCancellation";
import PasswordChanging from "@/components/User_setting/account_safety/password_changing/PasswordChanging";
import Phone_verification from "@/components/User_setting/account_safety/password_changing/PhoneVerification";
import store from "@/store";
import Fluid from "@/components/fluid"
import FluidAfter from "@/components/fluid-after"
import  Test from "@/components/book/test"
import  Comment from "@/components/book/comment"
import  Person from "@/components/book/person"
import OrderList from "../components/book/orderList";
import Bookput from "@/components/bookput";
import FBList  from "@/components/FBList"
import KeepList from "@/components/KeepList"
import Mine from "@/components/Mine";
import Map from"@/components/Map/map"
import Message from "@/components/Message/Message";
import MessageWatch from "@/components/Message/MessageWatch";
import CollectList from "@/components/book/CollectList";
import SubscribeList from "@/components/SubscribeAndFans/SubscribeList";
import FansList from "@/components/SubscribeAndFans/FansList";
import hot from "@/components/hot";
import SystemMessage from "@/components/Message/SystemMessage";
import ReflexWatch from "@/components/Message/ReflexWatch";
import YongHuDetail from "@/components/book/YongHuDetail";
import SendLetter from "@/components/Message/SendLetter";
import shareBook from "@/components/book/shareBook";
import fansList from "@/components/SubscribeAndFans/FansList";

//声明插件使用
Vue.use(VueRouter)


const router = new VueRouter({
    //所有路由
    routes:[
        {
           //默认路由
           path:"/",
           redirect:"/login"
        },
        {
            path:'/send-letter',
            component:SendLetter,
        },
        {
            path:"/register",
            component:Register,
            meta: {
                isAuth: false,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/login",
            component:Login,
            meta: {
                isAuth: false,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        // {
        //     path:"profile",
        //     component:Profile
        // },
        {
            path:"/book",
            component:Book,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/index",
            component: Index,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/user-edit",
            component: UserEdit,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/setting",
            component: Setting,
            name: "setting",
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
             path: "/bookDetail",
            name:"/bookDetail",
            component: BookDetail,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/fluid",
            component: Fluid,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/fluidAfter",
            component: FluidAfter,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },

        },
        {
            path: "/mine",
            component: Mine
        },
        {
            path: "/map",
            component: Map,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/message",
            component: Message,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/test",
            component: Test,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/address-edit",
            component: AddressEdit,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/my-receiving-address",
            component: MyReceivingAddress,
        },
        {
            path: "/receiving-address-edit",
            component: ReceivingAddressEdit,
            name: "receiving-address-edit",
        },
        {
            path: "/my-post-address",
            component: MyPostAddress,
        },
        {
            path: "/post-address-edit",
            component: PostAddressEdit,
            name: "post-address-edit",
        },
        {
            path: "/return-address-edit",
            component: ReturnAddressEdit,
            name: "return-address-edit",
        },
        {
            path: "/my-return-address",
            component: MyReturnAddress,
        },
        {
            path: "/account-safety",
            component: Account_safety,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/account-cancellation",
            component: AccountCancellation,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/phone-verification",
            component: Phone_verification,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/password-changing",
            component: PasswordChanging,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/collectList",
            component:CollectList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/SubscribeList",
            component:SubscribeList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/FansList",
            component:FansList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/bookput",
            component:Bookput,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/fblist",
            component:FBList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/person",
            name: "person",
            component: Person,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/collectList",
            component: CollectList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: "/orderList",
            component: OrderList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },

        },
        {
            path:"/comment",
            component:Comment,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/keeplist",
            component:KeepList,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/hot",
            component:hot,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/message-watch",
            component:MessageWatch,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/system-message",
            component:SystemMessage,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/reflex-watch",
            component:ReflexWatch,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/yonghuDetail",
            component:YongHuDetail,
            meta: {
                isAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path:"/shareBook",
            component:shareBook,
            meta: {
                isAuth: false,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },

    ]
})

//前置路由守卫
router.beforeEach((to,from,next) => {

    // eslint-disable-next-line no-undef
    if(to.meta.isAuth){ //判断是否需要鉴权
        if(store.state.userinfo.user){
            let username = store.state.userinfo.user.user.username;
            if(username && localStorage.getItem("username") === username){
                next();
            }
        }
        else{
            // this.$message.error("请先登录");
            alert("请先登录")
            next({
                path:'/login'
            });
        }
    }else{
        next()
    }
})
export default router